<?php 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\CabinetDataSecond;
use Illuminate\Support\Facades\Redis;

class DingxinTempController extends Controller
{
    protected $cacheKey = 'dingxintemp-66';

    public function getWeekData(Request $request){
        $cabinets = [
            '67' => [
                'name' => '1#柜',
                'points' => [
                    'a_2505457' => '1-1#二次电压',
                    'a_2505458' => '1-1#二次电流',
                    'a_2511523' => '1-1-B二次电压',
                    'a_2511524' => '1-1-B二次电流'
                ]
            ],
            '68' => [
                'name' => '2#柜',
                'points' => [
                    'a_2505508' => '1-2#二次电压',
                    'a_2505509' => '1-2#二次电流',
                    'a_2511533' => '1-2B二次电压',
                    'a_2511534' => '1-2B二次电流'
                ]
            ],
            '69' => [
                'name' => '3#柜',
                'points' => [
                    'a_2505522' => '1-3#二次电压',
                    'a_2505523' => '1-3#二次电流',
                    'a_2511544' => '1-3B二次电压',
                    'a_2511545' => '1-3B二次电流'
                ]
            ],
            '70' => [
                'name' => '4#柜',
                'points' => [
                    'a_2505532' => '1-4#二次电压',
                    'a_2505533' => '1-4#二次电流',
                    'a_2511568' => '1-4B二次电压',
                    'a_2511569' => '1-4B二次电流'
                ]
            ],
        ];

        $data = [];
        $days = [];
        for ($i=1; $i <= 7; $i++) { 
            $day = date('Y-m-d', strtotime("-{$i} day"));
            $days[] = $day;
            $data[$day] = [];

            $cacheVal = Redis::hget($this->cacheKey, $day);
            if ($cacheVal) {
                $data[$day] = json_decode($cacheVal,true);
                continue;
            }

            $start = strtotime($day.'00:00:00');
            $end = strtotime($day.'23:59:59');

            foreach ($cabinets as $cabinetId => $oneCabinet) {
                $oneData = [];
                $oneData['cabinet'] = $oneCabinet['name'];
                $oneData['data'] = [];
                foreach ($oneCabinet['points'] as $alias => $point) {
                    $val = CabinetDataSecond::getAvg($cabinetId, $alias, $start, $end);
                    array_push(
                        $oneData['data'], 
                        ['alias' => $alias, 'point' => $point, 'value' => $val]
                    );
                }

                array_push($data[$day], $oneData);
            }
            Redis::hset($this->cacheKey, $day, json_encode($data[$day]));
        }

        //清除过期缓存
        $cacheArr = Redis::hkeys($this->cacheKey);
        foreach ($cacheArr as $oneDay) {
            if (!in_array($oneDay, $days)) {
                Redis::hdel($this->cacheKey, $oneDay);
            }
        }

        return response()->json(['code'=>1, 'data' => $data, 'msg' => 'success']);
    }
}